<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/23 0023
 * Time: 16:33
 */

namespace frontend\models;


use yii\base\Model;

class BindForm  extends Model
{
    public $username;
    public $password;
    public $openid;
    public $rememberMe = true;

    //用于用户的绑定页面的表单模型
    public function rules()
    {
        return [
            [['username','password'],'required'],
            [['username','password'],'string'],
        ];
    }


    public function attributeLabels()
    {
        return [
            'username'=>'用户名',
            'password'=>'密码',
        ];
    }

    public function login(){
        //通过用户名字找到用户的信息
        $memberInfo = Member::findOne(['username'=>$this->username]);
        if($memberInfo==null){
            //添加错误信息
            $this->addError('username','用户不存在');
            return false;
        }
        //验证密码是否正确
        $rs = \Yii::$app->security->validatePassword($this->password,$memberInfo->password_hash);
        //如果用户的密码正确,则登录
        if($rs){
            $r = \Yii::$app->user->login($memberInfo);
            //登录成功后更新用户的信息
            $memberInfo->last_login_ip = ip2long(\Yii::$app->request->getUserIP());
            $memberInfo->last_login_time = time();
            $memberInfo->openid = $this->openid;
            $rs = $memberInfo->save(false);//保存信息
            //用户登录成功
        }else{
            $this->addError('password','密码错误');
        }
        return $rs;

    }
}